from main.models import Notice
from main import db
from sqlalchemy.exc import InvalidRequestError
from main.utils import JSONHelper
from datetime import datetime
from sqlalchemy import or_, and_
from flask import render_template

def insert(notice):
    try:
        notice.time = datetime.now()
        db.session.add(notice)
        db.session.commit()
        return True
    except InvalidRequestError:
        db.session.rollback()
    except Exception as e:
        print(e)
        return False


def query_user_id_is_notice(user_id, is_notice):
    return db.session.query(Notice).filter(and_(Notice.user_id == user_id, Notice.is_notice == is_notice)).all()


def query_user_id(user_id):
    return db.session.query(Notice).filter(Notice.user_id == user_id).first()


if __name__ == '__main__':
    print(query_user_id(1).to_json())




